package com.whalefin.controller.advertise;


import com.whalefin.domain.Advertise;
import com.whalefin.persistence.tools.PaginationResult;
import com.whalefin.persistence.tools.PaginationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
* Created by sf on 14-4-28.
*/
@Controller
@RequestMapping("/searchAdvertise")
public class SearchAdvertiseController {

	@Autowired
	private com.whalefin.service.AdvertiseService AdvertiseService;


    /**
     * 根据条件查询留言信息
     * @param pageNo
     * @param model
     * @return
     */
	@RequestMapping(method = RequestMethod.POST)
	public String login(Integer pageNo,Model model,HttpServletRequest request) {

        String ad_title = request.getParameter("ad_title");
        String status = request.getParameter("status");
        String createtime = request.getParameter("createtime");

        Advertise advertise = new Advertise();
        advertise.setCreatetime(createtime);
        advertise.setStatus(status);
        advertise.setAd_title(ad_title);

        //起始页
        if(pageNo == null){
            pageNo = 1;
        }else if(pageNo <= 0){
            pageNo = 1;
        }
        //默认每页显示几行
        int pageSize = PaginationUtils.DEFAULT_PAGESIZE;
        advertise.setPageSize(pageSize);
        //留言总数
        int totalCount = AdvertiseService.totalCountByCondition(advertise);
        int count = totalCount/pageSize;
        if(totalCount%pageSize > 0){
            count++;
        }
        if(count != 0 && pageNo >= count){
            pageNo = count;
        }
        int indexPage = PaginationUtils.getStartIndex(pageNo, pageSize);//显示当前页
        advertise.setIndexPage(indexPage);
        List<Advertise> advertiseList = AdvertiseService.queryAllAdvertiseByCondition(advertise);//分页查询所有留言
        PaginationResult<Advertise> page = new PaginationResult<Advertise>(advertiseList,totalCount,pageSize,indexPage);//分页封装类
        model.addAttribute("page",page);
        return "black/advertise/advertise";
    }
}
